package com.qsxc.system.exception;


import cn.hutool.core.collection.CollectionUtil;
import com.qsxc.business.bean.R;
import com.qsxc.system.log.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletResponse;

/**
 * Description:
 *
 * @Author:
 * @since: 2021-07-06
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public R handMissingServletRequestParameterException(HttpServletResponse response, MissingServletRequestParameterException ex) {
        return R.fail("缺少请求参数:" + ex.getParameterName());
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    public R handMethodArgumentNotValidException(HttpServletResponse response, MethodArgumentTypeMismatchException ex) {
        return R.fail("请求参数类型错误:" + ex.getName() + "类型错误");
    }

    @ResponseBody
    @ExceptionHandler(CustomException.class)
    public R handCustomException(HttpServletResponse response, CustomException ex) {
        log.error("GlobalExceptionHandler,code:{},msg:{}", ex.getCode(), ex.getMsg());
        log.info("错误代码" + response.getStatus());
        return R.fail(ex.getCode(), ex.getMsg());
    }

    @ResponseBody
    @ExceptionHandler(BindValidationException.class)
    public R handBindValidationException(HttpServletResponse response, BindValidationException ex) {
        log.error("GlobalExceptionHandler:", ex.getMessage());
        log.info("错误代码" + response.getStatus());
        return R.fail(ex.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R handException(HttpServletResponse response, Exception ex) {
        if (ex instanceof CustomException) {
            return R.fail(((CustomException) ex).getCode(), ((CustomException) ex).getMsg());
        }
        log.error("GlobalExceptionHandler:", ex);
        log.info("错误代码：" + response.getStatus());
        return R.fail("接口异常，请联系管理员");
    }

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handMethodArgumentNotValidException(MethodArgumentNotValidException methodArgumentNotValidException) {
        if (methodArgumentNotValidException.getBindingResult() != null) {
            if (!CollectionUtil.isEmpty(methodArgumentNotValidException.getBindingResult().getFieldErrors())) {
                String msg = methodArgumentNotValidException.getBindingResult().getFieldErrors().get(0).getDefaultMessage();
                LogUtil.info("系统参数验证:" + msg);
                LogUtil.printAll();
                return R.fail(msg);
            }
        }
        return R.fail("请填写必填项目！");
    }

}
